<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/29 0029
 * Time: 2:46
 */
namespace service\excel\feed;
use think\Config;
use think\Db;
use service\excel\AbstractExcel;
class feedSeat extends AbstractExcel
{
    public function __construct()
    {
        parent::__construct();
        $this->config = Config::get('feed');//这个配置写在app目录的config文件中，获取设定好的要的excel信息。
        /***
         * 配置信息如下
         *  'feed' => [
        'allow_pre' => ['POORT'],
        'feed_id' => 'A2',//工单号
        'head_materiel_id' => 'B2',//表头物料代码
        'pcb_id' => 'C2',//pcb板号
        'edition' => 'D2',//版本
        'effect_time' =>'E2',//生效日期
        'feed_num' => 'F2',//工单量
        'storage_name' => 'G2',//仓库名
         //以上是物料表的表头位置，下面的是详单的列的位置。行号的话，是获取最大值以后遍历过去。
        'begin' => 4,//详单开始的行号
        'machine_id' => 'A',//机台ID
        'table' => 'B',//TABLE字段
        'seat_code' => 'C',//站位号
        'L_R' => 'D',// 表上的L/R
        'sub_materiel_id' => 'E',
        'used_num' => 'F',//单位用量
        'size' => 'G', //供料器尺寸
        'remark' => 'H',//备注
        ],
         */

    }

    /***
     * 获取excel表中的数据
     * 料站表信息放在$this->feed里面
     * 料站细表的信息放在this->feedDetail里面
     */
    public function getData()
    {
        $reader = $this->phpReader->load($this->path);//读取上传的表格
        $sheet = $reader->getSheet(0);//获取第一个工作区
        $this->feed['feed_id'] = trim($sheet->getCell($this->config['feed_id'])->getValue());//工单号
        $this->feed['head_materiel_id'] = trim($sheet->getCell($this->config['head_materiel_id'])->getValue());//表头物料代码
        $this->feed['pcb_id'] = trim($sheet->getCell($this->config['pcb_id'])->getValue());//pcb板号
        $this->feed['edition'] = trim($sheet->getCell($this->config['edition'])->getValue());//版本
        $effect_time = trim($sheet->getCell($this->config['effect_time'])->getValue());//生效日期
        $this->feed['effect_time'] = gmdate('Y/m/d/h/m/s',\PHPExcel_Shared_Date::ExcelToPHP($effect_time));
        $this->feed['feed_num'] = trim($sheet->getCell($this->config['feed_num'])->getValue());//工单量
        $this->storageName = trim($sheet->getCell($this->config['storage_name'])->getValue());//仓库名
        $this->feed['status'] = 0;//状态：未完成

        $dataBegin = $this->config['begin'];//料站细表数据在 料站表模板.xls 文件中是从第四行开始的，文件第一行是料站表的表头，第二行是料站表的数据，第三行是料站细表的表头，第四行之后是料站细表的数据
        for ($i = $dataBegin; $i <= $sheet->getHighestRow(); $i++) {//以下是料站细表的数据
            $machineId = strtoupper(trim($sheet->getCell($this->config['machine_id'] . $i)->getValue()));
            $table = trim($sheet->getCell($this->config['table'] . $i)->getValue());
            $seatCode = trim($sheet->getCell($this->config['seat_code'] . $i)->getValue());
            $LR = trim($sheet->getCell($this->config['L_R'] . $i)->getValue());
            $subMaterielId = trim($sheet->getCell($this->config['sub_materiel_id'] . $i)->getValue());
            $usedNum = trim($sheet->getCell($this->config['used_num'] . $i)->getValue());
            $size = trim($sheet->getCell($this->config['size'] . $i)->getValue());
            $remark = trim($sheet->getCell($this->config['remark'] . $i)->getValue());
            $total_count = $usedNum * $this->feed['feed_num'];
            if (empty($machineId)||empty($seatCode)||empty($subMaterielId)) {
                break;
            }
            $this->feedDetail[] = [
                'uuid' => create_uuid(),
                'seat_code' => $seatCode,
                'machine_id' => $machineId,
                'L_or_R' => $LR,
                'sub_materiel_id' => $subMaterielId,
                'table'=> $table,
                'used_num' => $usedNum,
                'size' => $size,
                'remark' => $remark,
                'feed_id' => $this->feed['feed_id'],
                'status' => 1,
                'sent_count' => 0,
                'waste' => 0,
                'total_count' => $total_count
            ];
        }
    }

    /***
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 检查获取的各类数据是否有问题
     */
    public function checkData()
    {
        $this->getData();//获取数据
        $this->feed['storage_uuid'] = Db::table('iwh_storage')->where(['name' => $this->storageName])->value('uuid');//根据输入的仓库名获取仓库uuid
        if (empty($this->feed['storage_uuid'])) {
            return ret_template(false, "仓库{$this->storageName}不存在");
        }
        //检查这个feedId是否已存在
        $result = Db::table('smt_feed_station')->where(['feed_id'=>$this->feed['feed_id']])->find();
        if(!empty($result)){
            return ret_template(false, "工单号{$this->feed['feed_id']}已存在");
        }

        if(empty($this->feed['feed_id'])){
            return ret_template(false,"请填写工单号!");
        }
        if(empty($this->feed['feed_num'])){
            return ret_template(false,"请填写工单量!");
        }
        if(empty($this->feedDetail)){
            return ret_template(false, '请填写详细物料');
        }
        return ret_template(true,'','');//检查完毕，返回成功
    }

    public function insertToDb()
    {
        $feed = $this->feed;
        $detail = $this -> feedDetail;
        Db::startTrans();
        $feed['uuid'] = create_uuid();
        $tranBom = Db::table('smt_feed_station')->insert($feed);
        $tranDet = Db::table('smt_feed_station_detail')->insertAll($detail);
        if ($tranBom && $tranDet) {
            Db::commit();
            return ret_template(true, '成功生成单据');
        } else {
            Db::rollback();
            return ret_template(false, '生成单据失败');
        }
    }



}